<template>
    <div>
        <div>
            <div style="float:left">
                <Input style="width:250px" v-model="insName">
                <span slot="prepend">应用名称：</span>
                <Button slot="append" icon="ios-search" @click='findByInsName'></Button>
                </Input>
            </div>
    
            <Button-group>
                <Button @click="modal_add_status = true">新增</Button>
                <Button @click="exportData(2)">导出</Button>
            </Button-group>
        </div>
    
        <Table border stripe :columns="header" :data="data_list" size="small" ref="table" class="table" @on-row-click="rowClick"></Table>
    
        <Modal v-model="modal_add_status" title="添加一个应用" @on-ok="option_add_save('temp_add_data')" @on-cancel="modal_cancel" :loading="loadingStatus">
            <Form :model="temp_add_data" :label-width="90" :rules="ruleValidate" ref="temp_add_data">
                <Form-item label="应用名称：" prop="name">
                    <Input v-model.trim="temp_add_data.name" placeholder="请输入"></Input>
                </Form-item>
    
                <Form-item label="创建日期：">
                    <Date-picker type="date" placeholder="选择日期" v-model="temp_add_data.createTime"></Date-picker>
                </Form-item>
    
                <Form-item label="备注：">
                    <Input v-model.trim="temp_add_data.remark" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="请输入..."></Input>
                </Form-item>
    
            </Form>
        </Modal>
    
        <Modal v-model="modal_edit_status" title="编辑一个应用" @on-ok="option_edit_save('temp_edit_data')" @on-cancel="modal_cancel" :loading="loadingStatus">
            <Form :model.trim="temp_edit_data" :label-width="80" :rules="ruleValidate" ref="temp_edit_data">
                <Form-item label="应用名称：" prop="name">
                    <Input v-model="temp_edit_data.name" placeholder="请输入"></Input>
                </Form-item>
    
                <Form-item label="创建日期：">
                    <Date-picker type="date" placeholder="选择日期" v-model="temp_edit_data.createTime"></Date-picker>
                </Form-item>
    
                <Form-item label.trim="备注：">
                    <Input v-model="temp_edit_data.remark" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="请输入..."></Input>
                </Form-item>
    
            </Form>
        </Modal>
    
        <Modal v-model="modal_del_status" title="删除一个应用" @on-ok="option_delete_save" @on-cancel="modal_cancel">
            <Form :model="temp_dele_data" :label-width="80">
                <p>您确定删除这个记录吗？</p>
    
            </Form>
        </Modal>
    
    </div>
</template>
<script>
export default {
    data() {
        return {

            //初始化url
            init_url: '/mes/core/app/base/get/list',
            //添加一行url
            add_url: '/mes/core/app/base/add',
            //修改一行url
            edit_url: '/mes/core/app/base/update',
            //删除一行url
            del_url: '/mes/core/app/base/delete/',

            //表格标题
            header: [
                {
                    type: 'index',
                    width: 60,
                    align: 'center'
                },
                {
                    "title": "应用名称",
                    "key": "name",
                    "width": 120,
                    "sortable": true,
                },
                {
                    "title": "创建时间",
                    "key": "createTime",
                    "width": 120,
                    "sortable": true
                },
                {
                    "title": "备注",
                    "key": "remark",
                    "width": 150,
                    "sortable": true
                },
                {
                    "title": '操作',
                    "min-width": 100,
                    "align": 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.edit_show(params)
                                    }
                                }
                            }, '编辑'),
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                on: {
                                    click: () => {
                                        this.delete_show(params)
                                    }
                                }
                            }, '删除')
                        ]);
                    }
                }


            ],
            ruleValidate: {
                name: [
                    { required: true, message: '姓名不能为空', trigger: 'blur' }
                ],
            },
            //表格数据源
            data_list: [],
            //表格数据源缓存
            data_cache_list: [],
            //添加modal对话框显示状态
            modal_add_status: false,
            //编辑modal对话框显示状态
            modal_edit_status: false,
            //删除modal对话框显示状态
            modal_del_status: false,
            //modal手动关闭
            loadingStatus: true,
            //添加一行的临时数据
            temp_add_data: {
                name: '',
                createTime: '',
                remark: ''
            },
            //编辑一行的临时数据
            temp_edit_data: {
                id: '',
                name: '',
                createTime: '',
                remark: ''
            },
            //删除一行的临时数据
            temp_dele_data: {
                id: '',
                name: '',
                createTime: '',
                remark: ''
            },
            //删除一行的数据索引
            temp_dele_index: '',
            //编辑一行的数据索引
            temp_edit_index: '',
            //查询的名称
            insName: '',

        }
    },
    methods: {
        //到处excel
        exportData(type) {
            if (type === 1) {
                this.$refs.table.exportCsv({
                    filename: '原始数据'
                });
            } else if (type === 2) {
                this.$refs.table.exportCsv({
                    filename: '排序和过滤后的数据',
                    original: false
                });
            }
        },

        //modal对话框取消事件
        modal_cancel() {
            this.$Message.info('操作已经取消');
        },
        //编辑按钮触发事件
        edit_show(params) {
            this.temp_edit_data = Object.assign({}, params.row);
            this.modal_edit_status = true;
            this.temp_edit_index = params.index;

        },
        //删除按钮触发事件
        delete_show(params) {
            this.modal_del_status = true;
            this.temp_dele_data = params.row;
            this.temp_dele_index = params.index;
        },
        //添加一行操作
        option_add_save(form) {

            var _self = this;

            this.$refs[form].validate((valid) => {
                if (valid) {
                    this.$ajax.post(_self.add_url, this.temp_add_data)
                        .then(function (response) {
                            if (response) {
                                if (response.data.status == 'success') {
                                    _self.$Message.info('保存成功');                     
                                    _self.temp_add_data = response.data.data;
                                    _self.data_list.push(_self.temp_add_data);
                                    _self.data_cache_list.push(_self.temp_add_data);
                                    _self.modal_add_status = false;
                                    _self.temp_add_data = {
                                        name: '',
                                        createTime: '',
                                        remark: ''
                                    };
                                    return;
                                }
                            }
                            _self.$Message.info('保存失败');
                        })
                        .catch(function (error) {
                            console.log(error);
                            _self.$Message.info('保存失败');
                        });
                } else {
                    _self.$Message.info('表单填写错误');
                    
                }
            })

        },
        //修改一行操作
        option_edit_save(form) {
            var _self = this;

            this.$refs[form].validate((valid) => {
                if (valid) {
                    this.$ajax.post(_self.edit_url, this.temp_edit_data)
                        .then(function (response) {

                            var index = _self.$stringUtil.getStringIndex(_self.data_cache_list, _self.temp_edit_data.id);
                            _self.data_cache_list[index] = _self.temp_edit_data;

                            _self.data_list[_self.temp_edit_index] = Object.assign(_self.data_list[_self.temp_edit_index], _self.temp_edit_data);
                            _self.modal_edit_status = false;
                            _self.$Message.info('修改成功');
                        })
                        .catch(function (error) {
                            console.log(error);
                            _self.$Message.info('失败');
                        });
                } else {
                    _self.$Message.info('表单填写错误');
                    _self.modal_edit_status = true;
                }
            })
        },
        //删除一行操作
        option_delete_save() {
            var _self = this;
            this.$ajax.post(_self.del_url + this.temp_dele_data.id, )
                .then(function (response) {
                    if (response) {
                        console.log(response);
                        if (response.data.status == 'success') {
                            _self.$Message.info('删除成功');
                            _self.data_list.splice(_self.temp_dele_index, 1);
                            var index = _self.$stringUtil.getStringIndex(_self.data_cache_list, _self.temp_dele_data.id);
                            _self.data_cache_list.splice(index, 1);
                            return;
                        }
                        _self.$Message.info('删除失败');
                    }
                    _self.$Message.info('删除失败');
                })
                .catch(function (error) {
                    console.log(error);
                    _self.$Message.info('删除失败');
                });

        },
        //表格行点击事件
        rowClick(row) {
            //this.$emit('getAppId', row.id);
        },
        //根据学院名称查询学院
        findByInsName() {
            this.insName = this.$stringUtil.stringTrim(this.insName);
            var name = this.insName;

            if (name == "") {
                this.data_list = this.data_cache_list.slice();
                return;
            }

            var match = this.$stringUtil.stringMatch;
            this.data_list = $.grep(this.data_cache_list, function (value) {
                if (match(value.name, name)) {
                    return value;
                }
            });
        }

    },
    mounted() {

        var _self = this;

        //初始化表单数据
        this.$ajax.get(_self.init_url)
            .then(function (response) {
                _self.data_list = response.data;
                _self.data_cache_list = _self.data_list.slice();
            })
            .catch(function (error) {
                console.log(error);
            });
    }
}
</script>

<style>
.btn-group {
    margin-bottom: 5px;
}
</style>
